package com.gnote.repair.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gnote.repair.domain.LoginUser;
import com.gnote.repair.domain.SysUser;
import com.gnote.repair.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gnote
 */
@Service
@RequiredArgsConstructor
public class MyUserDetailsServiceImpl implements UserDetailsService {
    private final SysUserService sysUserService;

    /**
     * 实现loadUserByUsername方法
     *
     * @param username 用户名
     * @return 用户信息
     * @throws UsernameNotFoundException 用户不存在异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = sysUserService.getOne(Wrappers.lambdaQuery(new SysUser()).eq(SysUser::getUsername, username));
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        //查询用户的权限信息
        List<String> strings = sysUserService.getMenuListByDeptId(user.getId());
        return new LoginUser(user, strings);

    }
}